<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">

    <h:body>
        <ui:composition template="/resources/template/template.xhtml">
            <ui:define name="content">
                <h:form>



                    <p:panel>
                        <f:facet name="header" >
                            <h:outputLabel value="Bill List"/>                          
                        </f:facet>


                        <h:panelGrid columns="2" styleClass="alignTop">
                            <p:panel header="Search" >
                                <h:panelGrid columns="2" styleClass="alignTop">
                                    <h:outputLabel value="From"/>
                                    <p:calendar id="fromDate" value="#{searchController.fromDate}" navigator="false" pattern="dd/MMMM/yyyy hh:mm:ss a" >      
                                    </p:calendar>

                                    <h:outputLabel value="To"/>
                                    <p:calendar id="toDate" value="#{searchController.toDate}" navigator="false" pattern="dd/MMMM/yyyy hh:mm:ss a" >                                                                              
                                    </p:calendar>

                                    <h:outputLabel value="Bill No"/>
                                    <p:inputText autocomplete="off"  value="#{searchController.searchKeyword.billNo}" />

                                    <h:outputLabel value="Name"/>
                                    <p:inputText autocomplete="off" value="#{searchController.searchKeyword.patientName}" />




                                    <h:outputLabel value="Phone"/>
                                    <p:inputText autocomplete="off"  value="#{searchController.searchKeyword.patientPhone}" />

                                    <h:outputLabel value="Total"/>
                                    <p:inputText autocomplete="off"  value="#{searchController.searchKeyword.total}" />

                                    <h:outputLabel value="Net Total"/>
                                    <p:inputText autocomplete="off" value="#{searchController.searchKeyword.netTotal}"/>

                                    


                                </h:panelGrid>

                                <p:commandButton id="btnSearch" ajax="false" value="Search" action="#{searchController.createOpdBillSearch}"/>
                                <p:commandButton id="btnExcel" ajax="false" value="Excel" >
                                    <p:dataExporter fileName="OPD Bills" type="xls" target="tblBillsExport" ></p:dataExporter>
                                </p:commandButton>
                                <p:commandButton id="btnPrint" ajax="false" value="Print" >
                                    <p:printer target="tblBillsExport" ></p:printer>
                                </p:commandButton>
                                <p:defaultCommand target="btnSearch" />
                            </p:panel>


                            <p:dataTable rowIndexVar="i" id="tblBills" value="#{searchController.bills}" 
                                         var="bill"  
                                         style="min-width: 100%;"  
                                         paginator="true" rows="25"
                                         paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                                         rowsPerPageTemplate="5,10,25,50"
                                         >
                                <p:column headerText="No" styleClass="alignTop" width="15">
                                    <h:outputLabel value="#{i+1}"/>
                                </p:column>
                                <p:column headerText="Bill No" >                                
                                    <p:commandLink id="billNo" ajax="false" value="#{bill.insId}" action="#{searchController.viewOPD(bill)}">
                                        <f:setPropertyActionListener value="#{bill}" target="#{billSearch.billSearch}"/>
                                    </p:commandLink>
                                    <p:tooltip for="billNo">
                                        <p:panel header="Bill Details - Click to View" >
                                            <p:panelGrid columns="2" >
                                                <p:outputLabel value="Billed Date" ></p:outputLabel>
                                                <h:outputLabel value="#{bill.createdAt}" >
                                                    <f:convertDateTime pattern="dd/MMM/yyyy"/>
                                                </h:outputLabel>

                                                <p:outputLabel value="Billed Time" ></p:outputLabel>
                                                <h:outputLabel value="#{bill.createdAt.time}" >
                                                    <f:convertDateTime pattern="hh:mm:ss a"/>
                                                </h:outputLabel>

                                                <p:outputLabel value="Billed By" ></p:outputLabel>
                                                <h:outputLabel value="#{bill.creater.webUserPerson.nameWithTitle}" >
                                                </h:outputLabel>

                                                <p:outputLabel value="Cancellation" ></p:outputLabel>
                                                <p:outputLabel rendered="#{!bill.cancelled}" value="Not Cancelled" ></p:outputLabel>
                                                <h:panelGroup rendered="#{bill.cancelled}" >
                                                    <h:outputLabel style="color: red;" value="Cancelled at " />
                                                    <h:outputLabel style="color: red;" rendered="#{bill.cancelled}" value="#{bill.cancelledBill.createdAt}" >
                                                        <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm:ss a"/>
                                                    </h:outputLabel>
                                                </h:panelGroup>

                                                <p:outputLabel value="Refunds" ></p:outputLabel>
                                                <p:outputLabel rendered="#{!bill.refunded}" value="Not Refunded" ></p:outputLabel>
                                                <h:panelGroup rendered="#{bill.refunded}" >
                                                    <h:outputLabel style="color: red;" value="Refunded at " />
                                                    <h:outputLabel style="color: red;" rendered="#{bill.refunded}" value="#{bill.refundedBill.createdAt}" >
                                                        <f:convertDateTime pattern="dd/MMM/yyyy - hh:mm:ss a"/>
                                                    </h:outputLabel>
                                                </h:panelGroup>


                                            </p:panelGrid>
                                        </p:panel>

                                    </p:tooltip>
                                </p:column>

                                <p:column headerText="Billed At">
                                    <p:outputLabel value="#{bill.billDate}" >
                                        <f:convertDateTime pattern="dd-MM-yy h:m a"></f:convertDateTime>
                                    </p:outputLabel>
                                </p:column>

                                <p:column headerText="Bill No" rendered="#{sessionController.loggedPreference.opdBillPaperType eq 'PosPaper' and false}" >                                
                                    <h:outputLabel   value="#{bill.deptId}"/>
                                </p:column> 
                                <p:column headerText="Billed For"  >                                
                                    <h:outputLabel id="ins" value="#{bill.toDepartment.name}" />
                                    <p:tooltip for="ins" value="Billed for #{bill.toDepartment.name} of #{bill.toInstitution.name}." />
                                </p:column>
                                <p:column headerText="Collecting Centre"  >                                
                                    <h:outputLabel value="#{bill.collectingCentre.name}" />
                                </p:column>
                                <p:column headerText="Billed At"  >                                
                                    <h:outputLabel value="#{bill.department.name}" />
                                </p:column>
                                <p:column headerText="Billed by" >
                                    <h:outputLabel value="#{bill.creater.webUserPerson.name}" >                                      
                                    </h:outputLabel>
                                    <br/>
                                    <h:panelGroup rendered="#{bill.cancelled}" >
                                        <h:outputLabel style="color: red;" value="Cancelled By " />
                                        <h:outputLabel style="color: red;" rendered="#{bill.cancelled}" value="#{bill.cancelledBill.creater.webUserPerson.name}" >                                       
                                        </h:outputLabel>
                                    </h:panelGroup>
                                    <h:panelGroup rendered="#{bill.refunded}" >
                                        <h:outputLabel style="color: red;" value="Refunded By " />
                                        <h:outputLabel style="color: red;" rendered="#{bill.refunded}" value="#{bill.refundedBill.creater.webUserPerson.name}" >
                                        </h:outputLabel>
                                    </h:panelGroup>
                                </p:column>    
                                <p:column headerText="Client">                               
                                    <h:panelGroup id="pt">
                                        <h:commandLink  value="#{bill.patient.person.name}" action="patient" rendered="#{webUserController.hasPrivilege('OpdBillSearchEdit')}">
                                            <f:setPropertyActionListener value="#{bill.patient.person.nameWithTitle}" target="#{patientController.current}"/>
                                        </h:commandLink>
                                        <h:outputLabel value="#{bill.patient.person.name}" rendered="#{!webUserController.hasPrivilege('OpdBillSearchEdit')}">
                                        </h:outputLabel>
                                    </h:panelGroup>
                                    <p:tooltip for="pt">
                                        <p:panel header="Patient Details" >
                                            <p:panelGrid columns="2" >
                                                <p:outputLabel value="Name" ></p:outputLabel>
                                                <p:outputLabel value="#{bill.patient.person.nameWithTitle}" ></p:outputLabel>
                                                <p:outputLabel value="Address" ></p:outputLabel>
                                                <p:outputLabel value="#{bill.patient.person.address}" ></p:outputLabel>
                                                <p:outputLabel value="ID" ></p:outputLabel>
                                                <p:outputLabel value="#{bill.patient.phn}" ></p:outputLabel>
                                                <p:outputLabel value="Phone" ></p:outputLabel>
                                                <p:outputLabel value="#{bill.patient.person.phone}" ></p:outputLabel>
                                                <p:outputLabel value="Email" ></p:outputLabel>
                                                <p:outputLabel value="#{bill.patient.person.email}" ></p:outputLabel>
                                                <p:outputLabel value="Gender" ></p:outputLabel>
                                                <p:outputLabel value="#{bill.patient.person.sex}" ></p:outputLabel>
                                                <p:outputLabel value="Changing Details" ></p:outputLabel>
                                                <p:outputLabel value="Not Edited" rendered="#{bill.patient.person.editer eq null}"></p:outputLabel>
                                                <h:panelGroup rendered="#{bill.patient.person.editer ne null}" >
                                                    <h:outputLabel value="Edited By " />
                                                    <h:outputLabel rendered="#{bill.patient.person.editer ne null}" value="#{bill.patient.person.editer.webUserPerson.name}" >                                       
                                                    </h:outputLabel>
                                                    <h:panelGroup rendered="#{bill.patient.person.editedAt ne null}" >
                                                        <h:outputLabel value=" on " />
                                                        <h:outputLabel  rendered="#{bill.patient.person.editedAt ne null}" value="#{bill.patient.person.editedAt}" >
                                                            <f:convertDateTime pattern="dd/MMM/yyyy"/>
                                                        </h:outputLabel>
                                                        <h:outputLabel value=" at " />
                                                        <h:outputLabel  rendered="#{bill.patient.person.editedAt ne null}" value="#{bill.patient.person.editedAt}" >
                                                            <f:convertDateTime pattern="hh:mm:ss a"/>
                                                        </h:outputLabel>
                                                        <h:outputLabel value="." />
                                                    </h:panelGroup>
                                                </h:panelGroup>
                                            </p:panelGrid>
                                        </p:panel>
                                    </p:tooltip>
                                </p:column>
                                <p:column headerText="Phone">                              
                                    <h:outputLabel value="#{bill.patient.person.phone}" />
                                </p:column>
                                <p:column headerText="Payment" >                                
                                    <h:outputLabel value="#{bill.paymentMethod}" /><br/>
                                    <h:outputLabel value="#{bill.paymentScheme.name}" />
                                </p:column>
                                <p:column headerText="Gross Value" styleClass="averageNumericColumn">
                                    <h:outputLabel value="#{bill.total}" >
                                        <f:convertNumber pattern="#,##0.00"/>
                                    </h:outputLabel>
                                </p:column>
                                <p:column headerText="Discount" styleClass="averageNumericColumn">
                                    <h:outputLabel value="#{bill.discount}" >
                                        <f:convertNumber pattern="#,##0.00"/>
                                    </h:outputLabel>
                                </p:column>
                                <p:column headerText="Net Value" styleClass="averageNumericColumn">
                                    <h:outputLabel value="#{bill.netTotal}" >
                                        <f:convertNumber pattern="#,##0.00"/>
                                    </h:outputLabel>
                                </p:column>                         
                            </p:dataTable>
                        </h:panelGrid>


                    </p:panel>



                    <div>
                        <div class="printBlock" >
                            <p:dataTable rowIndexVar="i" id="tblBillsExport" value="#{searchController.bills}" rowKey="#{bill1.id}"
                                         var="bill1" paginator="false">
                                <p:column headerText="No" >
                                    <h:outputLabel value="#{i+1}"/>
                                </p:column>
                                <p:column headerText="Bill No" >   
                                    <p:outputLabel value="#{bill1.insId}" ></p:outputLabel>
                                </p:column>
                                <p:column headerText="Billed From Institution"  >                                
                                    <h:outputLabel value="#{bill1.institution.name}" />
                                </p:column>
                                <p:column headerText="Billed From Department"  >                                
                                    <h:outputLabel value="#{bill1.department.name}" />
                                </p:column>
                                <p:column headerText="Bill Date">
                                    <p:outputLabel value="#{bill1.billDate}" >
                                        <f:convertDateTime pattern="dd-MM-yy h:m a"></f:convertDateTime>
                                    </p:outputLabel>
                                </p:column>
                                <p:column headerText="Bill From" >                                
                                    <h:outputLabel   value="#{bill1.deptId}"/>
                                </p:column> 
                                <p:column headerText="Institution Billed For"  >                                
                                    <h:outputLabel value="#{bill1.toInstitution.name}" />
                                </p:column>
                                <p:column headerText="Department Billed For"  >                                
                                    <h:outputLabel value="#{bill1.toDepartment.name}" />
                                </p:column>
                                <p:column headerText="Collecting Centre"  >                                
                                    <h:outputLabel value="#{bill1.collectingCentre.name}" />
                                </p:column>
                                <p:column headerText="Billed At"  >                                
                                    <h:outputLabel value="#{bill1.institution.name} - #{bill.department.name}" />
                                </p:column>
                                <p:column headerText="Billed by" >
                                    <h:outputLabel value="#{bill1.creater.webUserPerson.name}" >                                      
                                    </h:outputLabel>
                                </p:column>
                                <p:column headerText="Calcelled" >
                                    <h:outputLabel value="Yes" rendered="#{bill1.cancelled}" ></h:outputLabel>
                                    <h:outputLabel value="No" rendered="#{!bill1.cancelled}" ></h:outputLabel>
                                </p:column>
                                <p:column headerText="Calcelled by" >
                                    <h:panelGroup rendered="#{bill1.cancelled}" >
                                        <h:outputLabel style="color: red;" rendered="#{bill1.cancelled}"
                                                       value="#{bill1.cancelledBill.creater.webUserPerson.name}" >                                       
                                        </h:outputLabel>
                                    </h:panelGroup>
                                </p:column>
                                <p:column headerText="Refunded" >
                                    <h:outputLabel value="Yes" rendered="#{bill1.refunded}" ></h:outputLabel>
                                    <h:outputLabel value="No" rendered="#{!bill1.refunded}" ></h:outputLabel>
                                </p:column>
                                <p:column headerText="Refunded by" >
                                    <h:panelGroup rendered="#{bill1.refunded}" >
                                        <h:outputLabel value="Refunded By " />
                                        <h:outputLabel rendered="#{bill1.refunded}" value="#{bill1.refundedBill.creater.webUserPerson.name}" >
                                        </h:outputLabel>
                                    </h:panelGroup>
                                </p:column>    
                                <p:column headerText="Client Name">                               
                                    <h:outputLabel value="#{bill1.patient.person.nameWithTitle}" />                                   
                                </p:column>
                                <p:column headerText="Client Phone No">                              
                                    <h:outputLabel value="#{bill1.patient.person.phone}" />
                                </p:column>
                                <p:column headerText="Client Email">                              
                                    <h:outputLabel value="#{bill1.patient.person.email}" />
                                </p:column>

                                <p:column headerText="Paymentmethod" >                                
                                    <h:outputLabel value="#{bill1.paymentMethod}" /><br/>
                                    <h:outputLabel value="#{bill1.paymentScheme.name}" />
                                </p:column>
                                <p:column headerText="Gross Value" styleClass="averageNumericColumn">
                                    <h:outputLabel value="#{bill1.total}" >
                                        <f:convertNumber pattern="#,##0.00"/>
                                    </h:outputLabel>
                                </p:column>
                                <p:column headerText="Discount" styleClass="averageNumericColumn">
                                    <h:outputLabel value="#{bill1.discount}" >
                                        <f:convertNumber pattern="#,##0.00"/>
                                    </h:outputLabel>
                                </p:column>
                                <p:column headerText="Net Value" styleClass="averageNumericColumn">
                                    <h:outputLabel value="#{bill1.netTotal}" >
                                        <f:convertNumber pattern="#,##0.00"/>
                                    </h:outputLabel>
                                </p:column>                         
                                <p:column headerText="Comments" >
                                    <h:outputLabel  value="#{bill1.comments}" >
                                    </h:outputLabel>
                                    <h:outputLabel rendered="#{bill1.refundedBill ne null}" value="#{bill1.refundedBill.comments}" >
                                    </h:outputLabel>
                                    <h:outputLabel  rendered="#{bill1.cancelledBill ne null}" value="#{bill1.cancelledBill.comments}" >
                                    </h:outputLabel>
                                </p:column>
                            </p:dataTable>
                        </div>
                    </div>

                </h:form>
            </ui:define>


        </ui:composition>

    </h:body>
</html>
